package chat.server.statemachine.mutex;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import chat.State;
import chat.statemachine.AbstractContent;
import chat.statemachine.AlgorithmActionInterface;
import chat.statemachine.AlgorithmActionInvocationException;
import chat.server.statemachine.ListOfAlgorithms;

public enum AlgorithmMutex implements AlgorithmActionInterface {
	TOKEN_MUTEX(MutexTokenContent.class) {
		public Object execute(final State state, final AbstractContent content)
				throws AlgorithmActionInvocationException {
			return AlgorithmMutexActions.receiveTokenContent(
					state, content);
		}
	},
	TOKEN_MUTEX_ASK(MutexTokenContent.class) {
		public Object execute(final State state, final AbstractContent content)
				throws AlgorithmActionInvocationException {
			return AlgorithmMutexActions.receiveMutexAskContent(
					state, content);
		}
	};

	private final static Map<Integer,AlgorithmMutex> privateMapOfActions = new HashMap<Integer, AlgorithmMutex>();
	public final static Map<Integer, AlgorithmMutex> mapOfActions = Collections
			.unmodifiableMap(privateMapOfActions);

	public final int algorithmOffset = 500;
	/* FIXME: not sure of this value */
	
	private final int actionIndex;
	private final Class<? extends AbstractContent> contentClass;

	static {
		for (AlgorithmMutex aa : AlgorithmMutex
				.values()) {
			privateMapOfActions.put(aa.actionIndex, aa);
		}
	}

	private AlgorithmMutex(
			final Class<? extends AbstractContent> msgClass) {
		this.actionIndex = ListOfAlgorithms.CHAT_SERVER_START_INDEX
				+ algorithmOffset + ordinal();
		this.contentClass = msgClass;
	}

	public int getActionIndex() {
		return actionIndex;
	}

	public Class<? extends AbstractContent> getContentClass() {
		return contentClass;
	}

	public String toString() {
		return actionIndex + ", " + contentClass.getName();
	}
}